//
//  PayManager.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/8/23.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "PayManager.h"
//支付宝的头文件
#import <AlipaySDK/AlipaySDK.h>

#import "APAuthInfo.h"
#import "APOrderInfo.h"
#import "APRSASigner.h"

//微信支付头文件
#import "WXApi.h"
#import "WXApiRequestHandler.h"
#import "WXApiManager.h"

@implementation PayManager

-(void)doAliPayWithData:(pay *)pay Success:(CommonBlock)success Error:(CommonVoidBlock)error
{
    // NOTE: 如果加签成功，则继续执行支付
    if (pay.pay_info != nil) {
        //应用注册scheme,在AliSDKDemo-Info.plist定义URL types
        NSString *appScheme = @"bugupayv1yeying";
        // NOTE: 调用支付结果开始支付
        [[AlipaySDK defaultService] payOrder:pay.pay_info fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
            
            if ( resultDic )
            {
                if ( [[resultDic objectForKey:@"resultStatus"] intValue] == 9000 )
                {
                    success(resultDic);
                    [HUDHelper alert:[NSString stringWithFormat:ASLocalizedString(@"支付成功")]];
                    
                }
                else
                {
                    error(resultDic);
                    [HUDHelper alert:ASLocalizedString(@"支付失败")];
                }
            }
            else
            {
                error(resultDic);
                [HUDHelper alert:ASLocalizedString(@"支付异常")];
            }
                                      
            
        }];
    }
}

-(void)doWechatPayWithData:(pay *)pay Success:(CommonBlock)success Error:(CommonVoidBlock)error
{
    //============================================================
    // V3&V4支付流程实现
    // 注意:参数配置请查看服务器端Demo
    // 更新时间：2015年11月20日
    //============================================================

    NSDictionary *dict = pay.wxpay_info;
    if(dict != nil){
        NSMutableString *retcode = [dict objectForKey:@"retcode"];
        if (retcode.intValue == 0){
            NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
            
            //调起微信支付
            PayReq* req             = [[PayReq alloc] init];
            req.partnerId           = [dict objectForKey:@"partnerid"];
            req.prepayId            = [dict objectForKey:@"prepayid"];
            req.nonceStr            = [dict objectForKey:@"noncestr"];
            req.timeStamp           = stamp.intValue;
            req.package             = [dict objectForKey:@"package"];
            req.sign                = [dict objectForKey:@"sign"];
            [WXApi sendReq:req];
            //日志输出
            NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
            return;
        }else{
            ;//
            [MBProgressHUD showTopTipMessage:[dict objectForKey:@"retmsg"]];
        }
    }else{
        [MBProgressHUD showTopTipMessage:ASLocalizedString(@"服务器返回错误，未获取到json对象")];
    }

}
@end
